﻿using System.Net;
using Ayatta.Contract;
using Enyim.Caching;
using System.Web.Mvc;
using Web.Base.Extensions;
using Web.Manage.Controllers;
using Web.Manage.Areas.Log.Models;

namespace Web.Manage.Areas.Log.Controllers
{
    public class ExceptionController : AdminBaseController
    {
        public ExceptionController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(int page = 1, int size = 20, byte level = 0, string machineIp = null, string server = null, string userName = null, string clientIp = null)
        {
            var model = new ExceptionControllerModel.Index();
            model.Level = level;
            model.MachineIp = machineIp;
            model.Server = server;
            model.UserName = userName;
            model.ClientIp = clientIp;
            model.Exceptions = LogService.GetPagedAppExceptions(page, size, level, machineIp, userName, clientIp).ToPagedList(page, size);
            return PartialView(model);
        }

        public ActionResult Detail(int param = 0)
        {
            var model = LogService.GetAppException(param);

            return PartialView(model);
        }

        public ActionResult Truncate()
        {
            var result = LogService.TruncateAppError();
            return result ? Json(HttpStatusCode.OK, "清除成功", false, "/log/exception", "/log/exception") : Json(HttpStatusCode.BadRequest, "清除失败", false, "/log/exception", "/log/exception");
        }
    }
}
